//
//  RightTableViewCell.m
//  Linkage
//
//  Created by LeeJay on 16/8/22.
//  Copyright © 2016年 LeeJay. All rights reserved.
//  代码下载地址https://github.com/leejayID/Linkage

#import "RightTableViewCell.h"

@interface RightTableViewCell ()

@end

@implementation RightTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        
        
//        CGFloat width = YYMenu_SCREEN_WIDTH - kLeftTableViewWidth - 10 - 10;
//
//        UIView *lastView = [[UIView alloc] init];
//        [self.contentView addSubview:lastView];
//        [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
////            make.left.mas_equalTo(self.contentView.mas_left).mas_offset(20);
////            make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-20);
//            make.centerX.mas_equalTo(self.contentView.mas_centerX).mas_offset(0);
//            make.centerY.mas_equalTo(self.contentView.mas_centerY).mas_offset(0);
//            make.width.mas_equalTo(width);
//            make.height.mas_equalTo(100);
//        }];
//        lastView.backgroundColor = [UIColor orangeColor];
    }
    return self;
}

-(void)setModel:(YYTopMenuModel *)model
{
    _model = model;
    _leftMargin = _leftMargin < 0 ? 10 : _leftMargin;
    CGFloat width = YYMenu_SCREEN_WIDTH - kLeftTableViewWidth - _leftMargin - _leftMargin;
    _itemLineCount = _itemLineCount < 0 ? 4 : _itemLineCount;
    _itemWidth = _itemWidth < 0 ? 70 : _itemWidth;
    
    if (!_autoSize) {
        if (_itemHorizontalMargin > 0) {//设置了水平间距,固定宽高时，自动计算宽度
            _itemWidth = (width - _itemHorizontalMargin * 3) / 4;
        }
        else
        {
            _itemHorizontalMargin = (width - _itemWidth * _itemLineCount) / (_itemLineCount - 1);
            if (_itemHorizontalMargin <= 0) {
                _itemHorizontalMargin = _itemHorizontalMargin < 0 ? 10 : _itemHorizontalMargin;
                _itemWidth = (width - _itemHorizontalMargin * 3) / 4;
            }
        }
    }
    
    _topMargin = _topMargin < 0 ? 10 : _topMargin;
    _bottomMargin = _bottomMargin < 0 ? 10 : _bottomMargin;
    _itemHorizontalMargin = _itemHorizontalMargin < 0 ? 10 : _itemHorizontalMargin;
    _itemVerticalMargin = _itemVerticalMargin < 0 ? 10 : _itemVerticalMargin;
    _itemHeight = _itemHeight < 0 ? 25 : _itemHeight;
    
    NSLog(@"_topMargin = %lf",_topMargin);
    NSLog(@"_bottomMargin = %lf",_bottomMargin);
    NSLog(@"_leftMargin = %lf",_leftMargin);
    NSLog(@"_itemLineCount = %ld",_itemLineCount);
    NSLog(@"_itemHorizontalMargin = %lf",_itemHorizontalMargin);
    NSLog(@"_itemVerticalMargin = %lf",_itemVerticalMargin);
    NSLog(@"_itemWidth = %lf",_itemWidth);
    NSLog(@"_itemHeight = %lf",_itemHeight);
    
    for (UIView *objectView in [self.contentView subviews]) {
        if ([objectView isKindOfClass:[UIButton class]]) {
            [objectView removeFromSuperview];
        }
    }
    
    
    NSArray *tagArr = model.TagArr;
    UIView *lastView = [UIView new];
    NSInteger line = 0;
    
    if (_autoSize) {//自适应
        BOOL isFirst = YES;
        for (NSInteger index = 0; index < tagArr.count; index++) {
            NSString *tagString = tagArr[index];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = index + 1;
            [self.contentView addSubview:button];
            [button setTitle:tagString forState:UIControlStateNormal];
            [button setTitleColor:rgbColor(0xffffff) forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:10];
            if (index == 0) {
                button.selected = YES;
                button.backgroundColor = SelectItemBgColor;
            }
            else
            {
                button.backgroundColor = NormalItemBgColor;
            }
            button.layer.cornerRadius = 5;
            CGFloat textWidth = [tagString yy_maxWidthWithFont:button.titleLabel.font Height:10];
            textWidth += 10;
            CGFloat textHeight = [tagString yy_maxHeightWithFont:button.titleLabel.font Width:MAXFLOAT];
            textHeight += 10;
            if (isFirst) {
                button.frame = CGRectMake(_leftMargin, line * (_itemHeight + _itemVerticalMargin) + _topMargin, textWidth, _itemHeight);
            }
            else
            {
                button.frame = CGRectMake(CGRectGetMaxX(lastView.frame) + _itemHorizontalMargin, line * (_itemHeight + _itemVerticalMargin) + _topMargin, textWidth, _itemHeight);
            }
            [button addTarget:self action:@selector(tagActionClicked:) forControlEvents:UIControlEventTouchUpInside];

            if (index + 1 < tagArr.count) {
                NSString *nextTagString = tagArr[index + 1];
                CGFloat nextTextWidth = [nextTagString yy_maxWidthWithFont:button.titleLabel.font Height:10];
                nextTextWidth += 10;
//                CGFloat nextMaxX = CGRectGetMaxX(button.frame) + _itemHorizontalMargin;
                CGFloat nextMaxX = CGRectGetMaxX(button.frame) + nextTextWidth;
                CGFloat width = YYMenu_SCREEN_WIDTH - kLeftTableViewWidth - _leftMargin - _leftMargin;
                if (nextMaxX > width) {
//                    CGFloat currentTagMaxX = CGRectGetMaxX(button.frame);
//                    NSLog(@"currentTagMaxX = %lf",currentTagMaxX);
//                    NSLog(@"nextMaxX = %lf",nextMaxX);
//                    NSLog(@"width = %lf",width);
//                    NSLog(@"超出屏幕范围，需要换行：%@,当前正在排版：%@",nextTagString,tagString);
//                    NSLog(@"nextTextWidth = %lf",nextTextWidth);
//                    NSLog(@"nextMaxX = %lf",nextMaxX);
                    isFirst = YES;
                    line++;
                    lastView.frame = CGRectMake(0, CGRectGetMaxY(button.frame) + _itemVerticalMargin, 0, 0);
                }
                else
                {
//                    NSLog(@"尚未超出屏幕范围，继续盘版：%@",tagString);
                    lastView.frame = button.frame;
                    isFirst = NO;
                }
            }
            lastView.frame = button.frame;
        }
        CGFloat cellHeight = CGRectGetMaxY(lastView.frame) + _bottomMargin;
        model.CellHeight = cellHeight;
    }
    else
    {//固定宽、高
        
        
        
        
        for (NSInteger i = 0; i < tagArr.count; i++) {
            NSString *tagString = tagArr[i];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = i + 1;
            [self.contentView addSubview:button];
            [button setTitle:tagString forState:UIControlStateNormal];
            [button setTitleColor:rgbColor(0xffffff) forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:10];
            if (i == 0) {
                button.selected = YES;
                button.backgroundColor = SelectItemBgColor;
            }
            else
            {
                button.backgroundColor = NormalItemBgColor;
            }
            button.layer.cornerRadius = 5;
            CGFloat textWidth = [tagString yy_maxWidthWithFont:button.titleLabel.font Height:10];
            textWidth += 10;
            CGFloat textHeight = [tagString yy_maxHeightWithFont:button.titleLabel.font Width:MAXFLOAT];
            textHeight += 10;
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.contentView.mas_left).mas_offset((i % _itemLineCount) * (_itemWidth + _itemHorizontalMargin) + _leftMargin);
                make.top.mas_equalTo(self.contentView.mas_top).mas_offset(i / _itemLineCount * (_itemHeight + _itemVerticalMargin) + _topMargin);
                make.width.mas_equalTo(_itemWidth);
                make.height.mas_equalTo(_itemHeight);
            }];

            [button addTarget:self action:@selector(tagActionClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        CGFloat cellHeight = (tagArr.count - 1) / _itemLineCount * (_itemHeight + _itemVerticalMargin) + _topMargin + _itemHeight + _bottomMargin;
        model.CellHeight = cellHeight;
    }
    
    
    
    
    
}

-(CGFloat)getCellHeight:(YYTopMenuModel *)model
{
    [self setModel:model];
    NSLog(@"self.model.CellHeight = %lf",self.model.CellHeight);
    return self.model.CellHeight;
}

-(void)tagActionClicked:(UIButton *)sender
{
    if (sender.selected) {
        
    }
    else
    {
        if (self.SelectItemBlock) {
            self.SelectItemBlock(sender.tag);
        }
        sender.selected = YES;
        sender.backgroundColor = SelectItemBgColor;
        
        for (UIButton *objectView in [self.contentView subviews]) {
            if ([objectView isKindOfClass:[UIButton class]] && objectView != sender) {
                objectView.selected = NO;
                objectView.backgroundColor = NormalItemBgColor;
            }
        }
        
        if (self.ItemBlock) {
            NSMutableDictionary *tempDic = [NSMutableDictionary new];
            tempDic[@"section"] = _sectionString;
            tempDic[@"content"] = sender.titleLabel.text;
            NSDictionary *resultDic = [NSDictionary dictionaryWithDictionary:tempDic];
            self.ItemBlock(resultDic);
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
